<template>
	<view class="detail-container">
		<view class="notice-card">
			<view class="notice-title">{{noticeInfo.title}}</view>
			<view class="notice-content rich-text-container">
				<rich-text :nodes="noticeInfo.content"></rich-text>
			</view>
			<view class="notice-meta">
				<text class="notice-time">{{formatDate(noticeInfo.createTime)}}</text>
				<text class="notice-author" v-if="noticeInfo.author">发布人：{{noticeInfo.author}}</text>
			</view>
			
		</view>
		
		<!-- 附件列表 -->
		<view class="attachment-box" v-if="noticeInfo.attachments && noticeInfo.attachments.length > 0">
			<view class="attachment-title">附件列表</view>
			<view 
				class="attachment-item" 
				v-for="(item, index) in noticeInfo.attachments" 
				:key="index"
				@click="downloadAttachment(item)"
			>
				<image class="attachment-icon" src="/static/icons/file.png"></image>
				<text class="attachment-name">{{item.name}}</text>
			</view>
		</view>
		
		<!-- 空状态 -->
		<view class="empty-box" v-if="!noticeInfo.id">
			<image class="empty-image" src="/static/empty.png"></image>
			<text class="empty-text">公告内容不存在或已删除</text>
		</view>
	</view>
</template>

<script>
import { getNoticeDetail } from '@/api/notice.js';
export default {
	data() {
		return {
			id: null,
			noticeInfo: {
				title: '',
				content: '',
				createTime: '',
				author: '',
				attachments: []
			}
		}
	},
	onLoad(options) {
		if (options.id) {
			this.id = options.id;
			this.loadNoticeDetail();
		} else {
			uni.showToast({
				title: '参数错误',
				icon: 'none'
			});
			setTimeout(() => {
				uni.navigateBack();
			}, 1500);
		}
	},
	methods: {
		// 加载公告详情
		async loadNoticeDetail() {
			uni.showLoading({
				title: '加载中'
			});
			
			try {
				const res = await getNoticeDetail(this.id);
				
				if (res) {
					this.noticeInfo = res.data;
				} else {
					uni.showToast({
						title: '公告不存在',
						icon: 'none'
					});
				}
			} catch (error) {
				console.error('加载公告详情失败', error);
				uni.showToast({
					title: '加载失败',
					icon: 'none'
				});
			} finally {
				uni.hideLoading();
			}
		},
		
		// 格式化日期
		formatDate(dateStr) {
			if (!dateStr) return '';
			
			const date = new Date(dateStr);
			return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')} ${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`;
		},
		
		// 下载附件
		downloadAttachment(item) {
			uni.showLoading({
				title: '下载中'
			});
			
			uni.downloadFile({
				url: item.url,
				success: (res) => {
					if (res.statusCode === 200) {
						// 保存到本地
						uni.saveFile({
							tempFilePath: res.tempFilePath,
							success: (result) => {
								uni.hideLoading();
								uni.showToast({
									title: '下载成功',
									icon: 'success'
								});
								// 打开文件
								uni.openDocument({
									filePath: result.savedFilePath,
									success: () => {
										console.log('打开文档成功');
									},
									fail: () => {
										uni.showToast({
											title: '无法打开此文件',
											icon: 'none'
										});
									}
								});
							},
							fail: () => {
								uni.hideLoading();
								uni.showToast({
									title: '保存失败',
									icon: 'none'
								});
							}
						});
					} else {
						uni.hideLoading();
						uni.showToast({
							title: '下载失败',
							icon: 'none'
						});
					}
				},
				fail: () => {
					uni.hideLoading();
					uni.showToast({
						title: '下载失败',
						icon: 'none'
					});
				}
			});
		}
	}
}
</script>

<style lang="scss" scoped>
.detail-container {
	min-height: 100vh;
	background-color: #f8f8f8;
	padding: 30rpx;
}

.notice-card {
	background-color: #fff;
	border-radius: 12rpx;
	padding: 30rpx;
	box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
}

.notice-title {
	font-size: 36rpx;
	font-weight: bold;
	color: #333;
	line-height: 1.4;
	margin-bottom: 20rpx;
}

.notice-meta {
	display: flex;
	align-items: center;
	flex-wrap: wrap;
	margin-bottom: 30rpx;
	color: #999;
	font-size: 24rpx;
}

.notice-time {
	margin-right: 20rpx;
}

.notice-content {
	font-size: 28rpx;
	color: #333;
	line-height: 1.7;
	word-break: break-word;
	overflow-wrap: break-word;
	word-wrap: break-word;
}

.attachment-box {
	background-color: #fff;
	border-radius: 12rpx;
	padding: 30rpx;
	margin-top: 30rpx;
	box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
}

.attachment-title {
	font-size: 30rpx;
	font-weight: bold;
	color: #333;
	margin-bottom: 20rpx;
}

.attachment-item {
	display: flex;
	align-items: center;
	padding: 20rpx 0;
	border-bottom: 1px solid #f0f0f0;
}

.attachment-icon {
	width: 40rpx;
	height: 40rpx;
	margin-right: 16rpx;
}

.attachment-name {
	font-size: 28rpx;
	color: #666;
	flex: 1;
}

.empty-box {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding-top: 200rpx;
}

.empty-image {
	width: 200rpx;
	height: 200rpx;
	margin-bottom: 20rpx;
}

.empty-text {
	font-size: 28rpx;
	color: #999;
}

/* 富文本内容样式 */
.rich-text-container {
	overflow-x: hidden;
}

.rich-text-container img {
	max-width: 100% !important;
	height: auto !important;
	display: block;
	margin: 10rpx auto;
}

.rich-text-container table {
	width: 100% !important;
	max-width: 100% !important;
	border-collapse: collapse;
	margin: 10rpx 0;
	border: 1px solid #e0e0e0;
}

.rich-text-container td, 
.rich-text-container th {
	border: 1px solid #e0e0e0;
	padding: 8rpx;
}

.rich-text-container p {
	margin: 10rpx 0;
}
</style> 